package com.example.bbs.controller.config;

import com.example.bbs.entity.Admin;
import com.example.bbs.entity.User;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author jiajiazi
 * @version 1.0
 * @date 2021/5/1 0:29
 */

public class AdminInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws IOException {
        try {
            System.out.println("admin登录拦截");
            //统一拦截（查询当前session是否存在user）(这里user会在每次登陆成功后，写入session)
            String requestURL = String.valueOf(request.getRequestURL());

            System.out.println(requestURL);
            Admin admin = (Admin) request.getSession().getAttribute("adminitor");
            if (admin != null) {
                System.out.println("session user true");
                return true;
            }
            System.out.println("session user false");
            response.sendRedirect(request.getContextPath() + "/admin");
        } catch (IOException e) {
            e.printStackTrace();
        }
        return false;
    }


    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
    }


    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }

}
